package com.biostime.qdingding.modules.qd.service;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

public class AlipaySignService {

	private static final String ALGORITHM = "RSA";
	
	public static final String RSA_PRIVATE = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALpnMmFuJf9xk8LHp4LxJmP8nW8canLaBt4LY/FImYVhNMiV15nreeKVI3IFRLu/pqLEmmNTY4KOBAJu1yuqtrOWRgFFRk9X42FBqhY0iyiYrIPo6k2DxvqxO0k6C02J1hpazImIZdpC2A+KOO2Lma9XIIOY42VhO/HKfmHwvYoDAgMBAAECgYEAnq7hXb32TQp0PpnWyYrfIrlXkNJA8HsY44gQ0cP7JWfGFogxHZAvl6tcnGFNzQLvKZtyzuGbUfXyBdw0ArF2i9+yAjRp0CQAvHX5iX2J8oS1lZtp1e/wfwtiHs/pWUnpY95l0NXvw1IyCGCSpRDbc5k+IhNYLDZrya+k2PhPHdECQQDxes8GODi6x1nsXo++Cyt/UJqm+sJA961Z+JHr9YonzULWBVcWTKUxVDHks41U/2j2LziwmxLJoKl3Ux79F7fbAkEAxZySi1p53W6xo4CZUk2t8+JDN1a2H6n/S3G/cLyuG/WpqDQEo6lJZVnXWSmrsCfX7dN+OmcYcqswx25qsnQC+QJAGsen1eS+Rdh93HzQLB9I/6GMxR/iExpkmym55RUh8TAOTXXiJpLxbvsLXXWcIuUAWuz5uHw6JSowIQ+SzDapKQJBAJc7Ex+Yt0jSTRpO8Yd+MVFC6KPyNWEYLWukepkhnLGDGTrDzIUfqi7pSZhP/rfKVkMJJ0I04g3nm2YUx40YoakCQQCBAX+VseOkcBOuxayM3ZhAq7e1SQvqCjwMMyaOZJUrAKPN1lFSBHUF5z4M6TPEsLxrTznUNylPn4D4BGw0jGY2";

	public static final String RSA_PUBLIC = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
	
	private static final String SIGN_ALGORITHMS = "SHA1WithRSA";

	private static final String DEFAULT_CHARSET = "UTF-8";

	public static String sign(String content, String privateKey) {
		try {
			PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
					Base64.decode(privateKey));
			KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
			PrivateKey priKey = keyf.generatePrivate(priPKCS8);

			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);

			signature.initSign(priKey);
			signature.update(content.getBytes(DEFAULT_CHARSET));

			byte[] signed = signature.sign();

			return Base64.encode(signed);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
